package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * ClassName: XiechengT3
 * Package: leetcode
 * Description:
 *
 * @Author wjn
 * @Create 2025/9/6 14:54
 * @Version 1.0
 */
public class XiechengT3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        for (int t = 0; t < T; t++) {
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = scanner.nextInt();
            }

            // Create m lists for each residue class
            List<Integer>[] lists = new ArrayList[m];
            for (int i = 0; i < m; i++) {
                lists[i] = new ArrayList<>();
            }

            // Group elements by residue: index i (0-indexed) has residue r = i % m
            for (int i = 0; i < n; i++) {
                int r = i % m;
                lists[r].add(a[i]);
            }

            // Sort each list in ascending order
            for (int i = 0; i < m; i++) {
                Collections.sort(lists[i]);
            }

            // Build the result array
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                int r = i % m;
                int k = i / m; // block index
                // Get the k-th element from the sorted list for residue r
                res[i] = lists[r].get(k);
            }

            // Output the result
            for (int i = 0; i < n; i++) {
                System.out.print(res[i]);
                if (i < n - 1) {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}
